using System;
using System.Collections;
using System.Collections.Generic;
using Main.Event;
using Main.Transition;
using UnityEngine;
using UnityEngine.Audio;
using ZSW.Framework;

namespace Main.Audio
{
    public class AudioManager : ZSWF_InitMonoBehaviour
    {
        [Header("音乐数据库")]
        [SerializeField] private SoundDetailsList_SO soundDetailData;
        [SerializeField] private SceneSoundList_SO sceneSoundData;
        [Header("声音源")]
        [SerializeField] private AudioSource AmbientSource;
        [SerializeField] private AudioSource GameSource;

        [Header("Audio Mixer")]
        public AudioMixer audioMixer;
        public AudioMixerSnapshot normalSnapshot;
        public AudioMixerSnapshot ambientSnapshot;
        public AudioMixerSnapshot muteSnapshot;

        private float TransitionTime = 8f;

        private float delay => UnityEngine.Random.Range(5f, 15f);
        private Coroutine soundCoroutine;

        public override void Init()
        {
            base.Init();
            MainEventSystem.AfterLoadSceneEvent += OnAfterLoadScene;
            MainEventSystem.PlaySoundEvent += OnPlaySound;
        }

        private void OnPlaySound(SoundName name)
        {
            if (soundDetailData.TryGetSoundDetails(name, out var soundDetails))
            {
                MainEventSystem.CallInitSoundEffect(soundDetails);
            }
        }

        private void OnAfterLoadScene()
        {
            string currentScene = SceneTransitionManager.CurrentScene;

            if (sceneSoundData.TryGetSceneSoundDetails(currentScene, out SceneSoundItem sceneSoundDetails))
            {
                if (soundDetailData.TryGetSoundDetails(sceneSoundDetails.music, out SoundDetails music) &&
                soundDetailData.TryGetSoundDetails(sceneSoundDetails.ambient, out SoundDetails ambient))
                {
                    if (soundCoroutine != null)
                    {
                        StopCoroutine(soundCoroutine);
                    }
                    soundCoroutine = StartCoroutine(PlaySoundCoroutine(music, ambient));
                }
            }
        }

        private IEnumerator PlaySoundCoroutine(SoundDetails music, SoundDetails ambient)
        {
            if (music != null && ambient != null)
            {
                PlayAmbientClip(ambient, 1f);
                yield return new WaitForSeconds(delay);
                PlayMusicClip(music, TransitionTime);
            }
        }

        private void PlayMusicClip(SoundDetails details, float TransitionTime)
        {
            audioMixer.SetFloat("MusicVolume", ConvertSoundVolume(details.soundVolume));
            GameSource.clip = details.soundClip;
            if (GameSource.isActiveAndEnabled)
            {
                GameSource.Play();
            }

            normalSnapshot.TransitionTo(TransitionTime);
        }

        private void PlayAmbientClip(SoundDetails details, float TransitionTime)
        {
            audioMixer.SetFloat("AmbientVolume", ConvertSoundVolume(details.soundVolume));
            AmbientSource.clip = details.soundClip;
            if (AmbientSource.isActiveAndEnabled)
            {
                AmbientSource.Play();
            }

            ambientSnapshot.TransitionTo(TransitionTime);
        }

        private float ConvertSoundVolume(float volume)
        {
            return (volume * 100 - 80);
        }
    }
}